/** elementui 相关封装  **/

Vue.component("ws-el-table",{  /// extParams 额外参数，可用于搜搜
	name:'ws-el-table',
	props:["pagesize","url","pagesizes","layout","pager","height","ext-params","firstPage"],
	data:function(){
		return {
			tableData:[],
			pageIndex:0,
			total:0,
			rows:40,
			loading:false,
			qsData:{}
		};
	},
	watch:{
		pagesize:function(nv,ov){
			this.rows = nv;
		},
		url: function(nv,ov)
		{
			this.reload();
		},
		extParams: function(nv,ov){
			this.reload();
		}
	},
	computed:{
		pgrpagesizes:function(){
			return this.pagesizes||[40, 100, 200, 400];
		},
		pgrlayout:function(){
			return this.layout||"total, sizes, prev, pager, next, jumper";
		}
	},
	mounted: function(){
	},
	methods:{
		loadData:function(qsData){
			this.loading = true;
			var page=this.pageIndex<1?1:this.pageIndex;
			var params = {page:page,rows:this.rows,order:0};
			if(qsData)
			{
				this.qsData = qsData;
			}
			Object.assign(params,this.qsData)
			if(this.extParams)
			{
				Object.assign(params,this.extParams);
			}
			var self = this;
			axios({
				url:this.url,
				method:'GET',
				params:params//{page:this.pageIndex-1,rows:this.rows}
			}).then(function(res){
				//self.$emit("on-table-data",res.data);
//				if(self.onTableData)
//				{
//					self.onTableData(res.data);
//				}
				self.$emit("ontabledata",res.data);
				self.loading=false;
				if(res.data.code==200)
				{
					self.tableData = res.data.data.rowData;
					self.total = res.data.data.record;
				}
				else{
					 self.$msgbox({title:"错误",message:"发生错误:"+res.data.msg,type:'error'})
				}
			}).catch(function(err){
				console.log(err);
				self.$msgbox({title:"错误",message:"发生异常",type:'error'})
			});
		},
		reload:function(){
			this.pageIndex = 1;
			this.total = 0;
			this.tableData = [];
			this.loading = true;
			this.loadData();
		},
		handleSizeChange:function(size){
			this.rows=size;
			this.loading = true;
			this.loadData();
		},
		handleCurrentChange:function(cur){
			this.pageIndex = cur;
			this.loading = true;
			this.loadData();
		}
	},
	template:'<div v-loading="loading"> \
				<slot v-bind:data="tableData" name="table"></slot> \
				<el-pagination v-if="!pager" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex" :page-sizes="pgrpagesizes" :page-size="rows" :layout="pgrlayout" :total="total"></el-pagination> \
			</div>'
});